public class 数值的整数次方 {

    boolean invalidInput = false;
    public double Power (double base, int exponent){

        if (equal(base,0.0)&&exponent <0){
            invalidInput = true;
            return 0.0;
        }
        int absexponent = exponent;
        if (exponent < 0){
           absexponent = -exponent;
        }
        double res = getPower(base,absexponent);
        if (exponent < 0){
            res = 1.0/res;
        }
        return res;
    }


    boolean equal(double num1,double num2){
        if (num1 - num2 >-0.000001&&num1-num2<0.000001){
            return true;
        }else {
            return false;
        }
    }
    //采用二分幂方式
    double getPower(double b,int e){
        if (e == 0){
            return 1.0;
        }
        if (e == 1){
            return b;
        }
        double result = getPower(b,e>>1);
        result*=result;
        if ((e&1)==1){  //这里&是按位与的意思，展开为二进制的时候，只有1和1相与才会得1，其他与的都是0，相与为1就证明都有一，是奇数来的
         result*=b;
        }
        return result;
    }


}
